import {
	defineStore
} from 'pinia'
export const useUserStore = defineStore('user', {
	state: () => {
		return {
			isLogin: uni.getStorageSync('token') ? true : false,
			userInfo: uni.getStorageSync('userinfo') || {}
		}
	},
	getters: {
		getLogin: (state) => state.isLogin
	},
	actions: {
		getUserInfo() {
			return new Promise(async (resolve, reject) => {
				let res = await uni.$u.api.getUserInfo()
				this.isLogin = true
				this.userInfo = res.userinfo
				// 获取设备标识符
				uni.getPushClientId({
					success: ({
						cid
					}) => {
						uniCloud.callFunction({
							name: 'uni-store-cid',
							data: {
								cid: cid,
								uid: this.userInfo.uid
							}
						}).then(res => {});
					},
					fail(err) {}
				})
				uni.setStorageSync('userName', res.show_username)
				uni.setStorageSync('userinfo', res.userInfo)
				resolve()
			})
		},
		// 退出登录
		getLogout() {
			let utype = uni.getStorageSync('utype')
			uni.clearStorageSync()
			uni.setStorageSync('utype', utype)
			this.userInfo = {}
			this.isLogin = false
			setTimeout(() => {
				uni.$u.route({
					type: 'tab',
					url: 'pages/home/home'
				})
			}, 1000)
		}
	},
	// #ifdef H5
	persist: {
		enabled: true,
		strategies: [{
			key: 'userInfo',
			storage: localStorage
		}]

	}
	// #endif
})